package com.dragon.mmochat.model.fuBen;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.dragon.mmochat.model.MmochatSerial;
import com.dragon.mmochat.model.enumType.MmochatFuBenType;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.service.MmochatMainService;
import com.dragon.mmochat.service.MmochatMapService;

public class MmochatFuBen implements Serializable {
	private static final long serialVersionUID = -1953986124039582997L;
	private static MmochatSerial serial = new MmochatSerial();

	// 副本ID
	private int id;

	// 副本类型
	private MmochatFuBenType type;

	// 副本创建时间
	private long createTime = System.currentTimeMillis();

	// 副本存在时间
	private long lifeTime;

	// 副本成员
	private List<Integer> owners = new ArrayList<Integer>();

	// 副本等级
	private int fuBenLevel;

	// 副本地图:第一个地图为入口地图!
	private List<MmochatMap> maps = new ArrayList<MmochatMap>();

	// 副本线路
	private int line;

	// 副本入口坐标
	private int entryX;
	private int entryY;

	// 是否所有成员为同一个帮派
	private boolean isGangFuBen = false;

	public MmochatFuBen() {
		id = serial.addAndGetSerialId();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public MmochatFuBenType getType() {
		return type;
	}

	public void setType(MmochatFuBenType type) {
		this.type = type;
	}

	public long getCreateTime() {
		return createTime;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}

	public long getLifeTime() {
		return lifeTime;
	}

	public void setLifeTime(long lifeTime) {
		this.lifeTime = lifeTime;
	}

	public List<Integer> getOwners() {
		return owners;
	}

	public void setOwners(List<Integer> owners) {
		this.owners = owners;
	}

	public void addOwner(int roleId) {
		if (!owners.contains(roleId)) {
			owners.add(roleId);
		}
	}

	public int getFuBenLevel() {
		return fuBenLevel;
	}

	public void setFuBenLevel(int fuBenLevel) {
		this.fuBenLevel = fuBenLevel;
	}

	public List<MmochatMap> getMaps() {
		return maps;
	}

	public void setMaps(List<MmochatMap> maps) {
		this.maps = maps;
	}

	public void addMap(MmochatMap map) {
		if (!maps.contains(map)) {
			maps.add(map);
		}
	}

	public int getLine() {
		return line;
	}

	public void setLine(int line) {
		this.line = line;
	}

	public int getEntryX() {
		return entryX;
	}

	public void setEntryX(int entryX) {
		this.entryX = entryX;
	}

	public int getEntryY() {
		return entryY;
	}

	public void setEntryY(int entryY) {
		this.entryY = entryY;
	}

	public boolean isTimeOut() {
		return System.currentTimeMillis() - createTime >= lifeTime;
	}

	public void close() {
		// 关闭副本地图，清除玩家，设置玩家副本ID
		for (Integer roleId : owners) {
			MmochatPlayer me = MmochatMainService.getRoleFromBuffer(roleId);
			if (me != null) {
				for (MmochatMap map : maps) {
					if (me.getMapId() == map.getMapId()) {
						// 送出副本地图
						me.leaveFuBen();
						MmochatMap newMap = MmochatMapService.getMapByMapId(4);
						MmochatMapService.tranToNewMap(map, me.getLine(),
								newMap, me.getLine(), 80, 708, me);
						break;
					}
				}
			}
		}
		owners.clear();

		// 关闭副本地图
		for (MmochatMap map : maps) {
			MmochatMapService.maps.remove(map.getMapId());
		}
		maps.clear();

		lifeTime = 0;
	}

	public boolean isGangFuBen() {
		return isGangFuBen;
	}

	public void setGangFuBen(boolean isGangFuBen) {
		this.isGangFuBen = isGangFuBen;
	}

}
